package com.tucue.adapter;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 * Created by tutu on 15/1/17.
 */
//public class GenericAdapter extends TypeAdapter<Object>

//public class GenericAdapter implements JsonSerializer<String>,
//        JsonDeserializer<String> {
//    public JsonElement serialize(String src, Type typeOfSrc,
//                                 JsonSerializationContext context) {
//        if (src == null) {
//            return new JsonPrimitive("");
//        } else {
//            return new JsonPrimitive(src.toString());
//        }
//    }
//    public String deserialize(JsonElement json, Type typeOfT,
//                              JsonDeserializationContext context)
//            throws JsonParseException {
//        return json.getAsJsonPrimitive().getAsString();
//    }
//}

public class GenericAdapter extends TypeAdapter<Object>
{
    @Override
    public void write(JsonWriter jsonWriter, Object o) throws IOException
    {
        jsonWriter.beginObject();
        for (Field field : o.getClass().getDeclaredFields())
        {
            Object fieldValue = runGetter(field, o);
            jsonWriter.name(field.getName());
//            if(field.getName().contains("time"))
//            {
//               //String str=fieldValue.toString();
//               //String fin= str.substring(0,10);
//                jsonWriter.value("ssss");
//            }
            if (fieldValue == null)
            {
                jsonWriter.value("");
            }
            else {
                if(field.getName().contains("time"))
                {
                    //String str=fieldValue.toString();
                    DateFormat df   =   new SimpleDateFormat( "yyyy-MM-dd   HH:mm:ss ");
                    String time =df.format(fieldValue);
                    jsonWriter.value(time.substring(5,time.length()));

                }
                else
                    jsonWriter.value(fieldValue.toString());
            }
        }
        jsonWriter.endObject();
    }

    @Override
    public Object read(JsonReader jsonReader) throws IOException
    {
    /* Don't forget to add implementation here to have your Object back alive :) */
        return null;
    }

    /**
     * A generic field accessor runner.
     * Run the right getter on the field to get its value.
     * @param field
     * @param o {@code Object}
     * @return
     */
    public static Object runGetter(Field field, Object o)
    {
        // MZ: Find the correct method
        for (Method method : o.getClass().getMethods())
        {
            if ((method.getName().startsWith("get")) && (method.getName().length() == (field.getName().length() + 3)))
            {
                if (method.getName().toLowerCase().endsWith(field.getName().toLowerCase()))
                {
                    try
                    {
                        return method.invoke(o);
                    }
                    catch (Exception e)
                    { }
                }
            }
        }
        return null;
    }
}